//  2016年8月17日 星期三  HTML数据格式
//module.exports = html;//  模仿 npm包
//实现一个非;结尾的脚本
const  fs = require('fs'),
       util = require('util')
module.exports = function(){
    this.header = ""
    this.body = ""
    this.tpl = ""
    //  网页区分移动端和PC端
    this.doctype = "<!DOCTYPE html><html>"
    //  移动端设置>>并动态为HTML类添加 cli对象,
    this.isMobile = function(req,tool){
        if(typeof(tool) == 'object' && typeof(req) == 'object'){
            this.cli = tool.client(req)
            if(this.cli.isMobile()){
                this.doctype = `
                    <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml">
                `
            }
        }
        
    }
    this.title = function(title){
        title = typeof(title) == 'undefined'? '':title
        return '<title>'+title+'</title>'
    }
    this.headInit = function(){
        return `
            ${this.doctype}
            <head>
                ${this.header}
            </head>
        `
    }
    this.bodyInit = function(){
        return `
            <body>
                ${this.body}
            </body>
            </html>
        `
    }
    this.appHead = function(html){
        var oldhtml = this.header
        if(html) oldhtml += html
        this.header = oldhtml
        return oldhtml
    }
    this.appBody = function(html){
        var oldhtml = this.body
        oldhtml += html
        this.body = oldhtml
        return oldhtml
    }
    this.load = function(opt){
        if(typeof(opt) == 'undefined') return false
        var xhtml = ''
        if(opt['title']) xhtml += this.title(opt['title'])
        if(opt['js']){
            var js = typeof(opt['js']) == 'string'? [opt['js']]:opt['js']
            for(var k in js){xhtml += '<script src="'+js[k]+'.js"></script>'}
        }
        if(opt['css']){
            var css = typeof(opt['css']) == 'string'? [opt['css']]:opt['css']
            for(var k in css){xhtml += '<link href="'+css[k]+'.css" type="text/css" rel="stylesheet">'}
        }
        if(opt['jsscript']){
            xhtml += '<script>'+opt['jsscript']+'</script>'
        }
        this.appHead(xhtml)
        return xhtml
    }
    this.html = function(file){
        this.html = this.fileString(file)
        return this.html
    }
    this.fileString = function(file){
        if(fs.existsSync(file)) return fs.readFileSync(file).toString()
        else return ""
        /*
         var ctt = {'ret':''}
        //  异步对方服务返回
        fs.exists(file,(exists)=>{
            if(exists){
                ctt['ret'] = fs.readFileSync(file).toString()
            }
            else ctt['ret'] = 'Error'
            //文件读取失败
        });
        return ctt['ret']
        */
    }
    this.getHtml = function(){
        var html = `
        ${this.doctype}
        <head>
            ${this.header}
        </head>
        <body>
            ${this.body}
        </body>
        </html>`
        this.tpl = html
        return html
    }
    /**********************HTML 表单处理 */
    //  HTML的标准输入框
    this.text = function(input){
        var option = [],
            data = {'type':'text','value':''},
            html = '',
            opt
        if(!util.isArray(input)) option = [input]
        else option = input
        for(var k in option){
            opt = option[k]
            if(!this.empty(opt) && opt.type) data['type'] = opt.type
            if(!this.empty(opt) && opt.value) data['value'] = ` value="${opt.value}"`
            html += `<input type="${data.type}"${data.value}`
            if(!this.empty(opt)){
                for(var k in opt){
                    if(k == 'type') continue
                    else if(k == 'value') continue
                    html += ' '+k+'="'+opt[k]+'"'
                }
            }
            html += '>'
            if(!this.empty(opt) && opt.text && ('checkbox' == opt.type || 'radio' == opt.type)) html += opt.text
        }
        return html
    }
    //  单select元素
    this.select = function(input){
        if(this.empty(input)) return ''
        var html = '',option = '',ojn,select
        if(!this.empty(input) && input.option){
            ojn = input.option
            for(var k in ojn){
                select = ojn[k]['selected']? ' selected="selected"':''
                option += `<option value="${ojn[k]['value']}"${select}>`+(ojn[k]['text']? ojn[k]['text']:ojn[k]['value'])
            }
        }
        if(this.empty(option)) return ''
        if(!input.disempty) option = '<option value="">'+option
        else if(input.desc) option = '<option value="">'+input.desc+option
        html = '<select'
        if(input.id) html += ' id="'+input.id+'"'
        if(input.class) html += ' class="'+input.class+'"'
        html += '>'+option+'</select>'
        return html
    }
    //  单textarea元素
    this.textarea = function(input){
        var html = '<textarea'
        if(util.isObject(input)){
            for(var k in input){
                if('text' != k) html += ` ${k}="${input[k]}"`
            }
            if(input['text']) html += `>${input['text']}`      
        }else if(util.isString(input)) html += `>${input}`
        else html += '>'
        html += '</textarea>'
        return html
    }
    /*
        测试示例
        
        echo(h.text([
            {'type':'checkbox','value':Math.random(),'text':'UY'},
            {'type':'button','value':Math.random()}
        ]));
        echo(h.select({
            id:'test',class:'t',desc:"select元素测试",
            disempty:true,
            option:[
                {'value':'1','text':'杨'},
                {'value':'2','selected':true},
                {'value':'3'}
            ]
        }));
    */
    this.empty = function(value){
        if(typeof(value) == 'undefined') return true
        else if(null == value) return true
        else if('' == value) return true
        return false
    }
}






/*
const  fs = require('fs')
module.exports = () => {
    //  头部
    this.header = ''
    //  主体
    this.body = ''
    //  HTML模板
    this.tpl = `
        <!DOCTYPE html>
        <html>
        <head>
            ${this.header}
        </head>
        <body>
            ${this.body}
        </body>
        </html>
    `
    //  标题
    this.title = (title) =>{
        title = typeof(title) == 'undefined'? '':title
        return '<title>'+title+'</title>'
    }
    this.appHead = (html) =>{
        var oldhtml = this.header
        if(html) oldhtml += html
        this.header = oldhtml
    } 
    //  前端加载工具
    this.load = (opt) => {
        if(typeof(opt) == 'undefined') return false
        var xhtml = ''
        if(opt['title']) xhtml += this.title(opt['title'])
        if(opt['js']){
            var js = typeof(opt['js']) == 'string'? [opt['js']]:opt['js']
            for(var k in js){xhtml += '<script src="'+js[k]+'.js"></script>'}
        }
        if(opt['css']){
            var css = typeof(opt['css']) == 'string'? [opt['css']]:opt['css']
            for(var k in css){xhtml += '<link href="'+css[k]+'.css" type="text/css" rel="stylesheet">'}
        }
        if(opt['jsscript']){
            xhtml += '<script>'+opt['jsscript']+'</script>'
        }
        this.appHead(xhtml)
        return xhtml
    }
    //  以文件为视图
    this.html = (file) =>{
        fs.exists(file,(exists)=>{
            if(exists) return fs.readFileSync(file)
            else return ''
        });
        return ''
    }
    this.getHtml = () =>{return this.tpl}
}
*/